//
//  UIView+AddAction.m
//  lvfj
//
//  Created by lvfeijun on 2021/7/6.
//  Copyright © 2021 Goudu KuaiEx Technology(Shenzhen)Co.,ltd. All rights reserved.
//

#import "UIView+AddAction.h"
#import <objc/runtime.h>

@implementation UIView (AddAction)

- (void)hq_addtouchAction:(TouchViewAction)block
{
    objc_setAssociatedObject(self, @selector(hq_addtouchAction:), block, OBJC_ASSOCIATION_COPY_NONATOMIC);
       self.userInteractionEnabled=YES;
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(blockAction)];
    [self addGestureRecognizer:tap];
}

- (void)blockAction
{
    TouchViewAction block = objc_getAssociatedObject(self, @selector(hq_addtouchAction:));
    if (block) {
        block(self);
    }
}
//长按事件
- (void)hq_addLongTouchAction:(TouchViewAction)block
{
  
    objc_setAssociatedObject(self, @selector(hq_addLongTouchAction:), block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    self.userInteractionEnabled=YES;
    UILongPressGestureRecognizer *tap=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longBackAction:)];
    [self addGestureRecognizer:tap];
}

- (void)longBackAction:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state==UIGestureRecognizerStateBegan) {
        TouchViewAction block = objc_getAssociatedObject(self, @selector(hq_addLongTouchAction:));
        if (block) {
            block(self);
        }
    }
    
}
@end
